package org.example.tliaswebmanagement.controller;


import org.example.tliaswebmanagement.pojo.Emp;
import org.example.tliaswebmanagement.pojo.Result;
import org.example.tliaswebmanagement.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {


    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {


        return Result.success(empService.page(page, pageSize, name, gender, begin, end));
    }


    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {

        empService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {

        return Result.success(  empService.getById(id));
    }

    @PutMapping
    public Result update(@RequestBody Emp emp) {

        empService.updateEmp(emp);
        return Result.success();
    }

    @PostMapping
    public Result addEmp(@RequestBody Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empService.addEmp(emp);
        return Result.success();
    }









}
